/**
 * 职权目录初始化模块，提供职权目录列表及编辑页面初始化组件功能
 * 
 * @Date: 2016-1-25
 * @author: Jackson wang
 * @version: 1.0
 * @since: 1.0
 */

define(["jquery","sysmanMain","datagridServ","ajaxServ","ckeditor-jquery","deptUserSelect","valueUtil","formServ"],
		function($,sysmanMain,datagridServ,ajaxServ,ckeditor,deptUserSelect,valueUtil,formServ) {
			
		'use strict';
		var contentsid = 'contents',lob_contentsid = 'lob_contents',
			contentEl,lob_contentEl,
			editor1,
			$edit_window_height=600,$edit_window_width=900,
			uploadListurl = '/upload/query/getListDomain.action',
			imageListurl = '/upload/query/getImageListDomain.action',
			uploadurl='/upload/uploadCkeditor.action',
			
			imageUploadurl = '/upload/uploadCkeditorImage.action',
			docdetailEditUrl='/document/query/moduleEditPage.action',
			//窗口id
			window_id ,
			
			//扩展属性字段名称
			extendMsg = 'form_content'
		var public_method ={
			
			//列表页面加载数据预处理
			detailLoadFilter:function(data){
				
				if(data && data.rows && data.rows.length>0){
					$.each(data.rows,function(i,r){
						if(r && r[extendMsg]){
							try{
								var _data = $.parseJSON(r[extendMsg]);
								$.extend(r,_data);
							}catch(e){
								if(window.console)
									console.info('data exception ...')
							}
						}
					})					
				}
				return data;
			},
			/**
			 * 
			 * @param _options.grid	  文档列表
			 * @param _options.flag	  新增或编辑标志
			 * @param _options.window	编辑窗口
			 * @param _options.typeid 分类id
			 */
			pageShow:function(_options){
				var _pageConfig = {
					width:$edit_window_width,
					height:$edit_window_height
				}
				
				$.extend(_pageConfig,_options)
				
				
				var _url
				var _config = sysmanMain.getCurrentModuConfig();
				if(_config.addition_param){
					var _code = $.parseJSON(_config.addition_param)
					if(_code.module){
						_url = '/'+sysmanMain.getRootPath() +docdetailEditUrl +'?module='+_code.module
					}else{
						_url = '/'+sysmanMain.getRootPath() + docdetailEditUrl
					}
				}
				  
				
				var _param = {
					grid:_options.grid,
					window:_options.window,
					href:_url,
					width:_pageConfig.width,
					height:_pageConfig.height,
					flag:_options.flag,
					existCheckElement:'form'					
				}
				if(_options.onWindowClose){
					_param.onClose=_defaultConfig.onWindowClose
				}else{
					_param.onClose = public_method.onClose
				}
				datagridServ.windowEdit(_param);
			},
			
			rowClick:function(index,data){
				
				if(window.console)
					console.info('docDuties rowClick event fired ...')
				var _config = sysmanMain.getModuConf('inpatientItemFee');
				if(_config ==null){
					_config = {}					
				}	
				
				_config.initQueryParams = {field:'inpatient_no',fieldVal:data.inpatient_no}
				sysmanMain.setModuConf('inpatientItemFee',_config)
				sysmanMain.medicalInpatientItemFeeSelect()
			},
			/**
			 * 初始化新增页面
			 * @param _options.record 列表选中行
			 * @param _options.edit_window 页面form对象
			 * @param _options.rootId 页面根节点
			 */
			initElement:function(_options){
				
				if(window.console)
					console.info('docDuties edit loaded and starting ...')
				var _rootpath = '/'+sysmanMain.getRootPath()
				
				var lobQueryUrl='/lob/getOne.action'			
				
				//人员选择
				window_id =  _options.edit_window
				
				var _el = $('#'+window_id+' #public_user_name')
				if(_el &&_el.length>0){
					
					$(_el).textbox({
						onClickButton:function(data){
							var _userParam = {
								userids:window_id +' #public_user_id',
								usernames:window_id+' #public_user_name',
								deptid:window_id +' #public_dept_id',
								deptname:window_id+' #public_dept_name',
								selectByOwner:true,
								subsystem:'document'
							}
							deptUserSelect.openPage(_userParam)							
						}
					})
					
					var txtValue = $(_el).textbox('getValue')
					if(!txtValue || $.trim(txtValue)==''){
						var usr = sysmanMain.getCurrentUser();
						$(_el).textbox('setValue',usr.name)
						$('#'+window_id+' #public_user_id').val(usr.id)
						
						$('#'+window_id+' #public_dept_name').textbox('setValue',usr.deptName)
						$('#'+window_id+' #public_dept_id').val(usr.deptid)
					}
				}
				
				//时间自动赋值				
				_el = $('#'+window_id+' #public_date')
				if(_el &&_el.length>0){
					var oldValue = $(_el).datebox('getValue');
					if(!oldValue){
						var curDate = new Date();
						var _tmp = valueUtil.getDateValue({source:curDate,format:'yyyy-MM-dd HH:mi:ss'})
						$(_el).datetimebox('setValue',_tmp);
					}
				}
				//判断form对象是否存在
				var lobinitialized = false
				if(_options.record){
					if(_options.record.lobid){
						var _lobParam = {
							url :'/'+sysmanMain.getRootPath()+lobQueryUrl,
							data:{								
								id:_options.record.lobid								
							},
							success:function(data){
								private_method.setLobContents({
									data:data,
									edit_window:_options.edit_window
								})
							}
						}
						
						$.ajax(_lobParam);	
						lobinitialized = true;
					}
					
					//加载扩展form属性
					var ef = $('#'+window_id + ' form.main-form')
					if(ef.length == 1 && _options.record[extendMsg]){
						var _d = _options.record[extendMsg]
						if(_d){
							if($.trim(_d).length>0){
								var _c = $.trim(_d).substring(0,1)
								if(_c && _c!='{'){
									_d = '{'+_d+'}'
								}
							}
							
							var _data = $.parseJSON(_d)
							if(_data ){
								if(_data.contents){
									ef.form('load',_data.contents)
								}else{
									ef.form('load',_data)
								}
							}								
						}
					}
				}
				
				//设置扩展表单属性
				
//				if(!lobinitialized){
//					private_method.initlob();
//				}
				
				
			},
			
			/**
			 * 提交前执行的数据处理函数
			 */
			beforeSubmit:function(){
				
				var retVal = {}
				
				//获取ckeditor内容
				if(editor1){
					retVal.lob_contents =editor1.getData()
				}
				
				//获取扩展表单属性
				var ef = $('#'+window_id + ' form.main-form')
				if(ef.length == 1){
					var _data = formServ.getDataObj({form:ef})
					
					if(_data.createTime){
						retVal.createTime = public_method.checkDateTime(_data.createTime);	
					}
					_data.lob_contents=null;
					retVal[extendMsg] = $.toJSON(_data);
				}
					
				return retVal
			},
			
			/**
			 * 检查日期时间格式是否正确
			 */
			checkDateTime:function(data){
				var ret = data;
				if(data){
					if(data.length <= 10){
						ret = data.substring(0,10) + ' 00:00:00';						
					}
				}
				return ret;
			}
			,
			
			onClose:function(){
				if(editor1){
					editor1.destroy()
					editor1 = null
				}
				$('#' +window_id).empty();
				window_id = null;
				
			}
			
		}
		
		var private_method ={
			
			/**
			 * 设置大字段内容
			 * @param _options.edit_window
			 * @param _options.data
			 * @param _options.contentsid
			
			 * @param _options.lob_contentEl
			 */
			setLobContents:function(_options){
				
				
				var defaultConfig={
					edit_window:null,
					data:null,
					contentsid:contentsid,
					lob_contentsid:lob_contentsid
				}
				
				$.extend(defaultConfig,_options)
				
				if(defaultConfig.edit_window && defaultConfig.data){
									
					private_method.initlob(defaultConfig.data.lob_contents)
//									
				}
			},
			initlob:function(data){
				var config={
					filebrowserBrowseUrl:'/'+sysmanMain.getRootPath()+uploadListurl,
					filebrowserImageBrowseUrl:'/'+sysmanMain.getRootPath()+imageListurl,
					filebrowserUploadUrl:'/'+sysmanMain.getRootPath()+uploadurl,
					filebrowserImageUploadUrl:'/'+sysmanMain.getRootPath()+imageUploadurl
				}
				editor1 = CKEDITOR.appendTo(lob_contentsid,config,data)
					
			}
		}
			
		
		return public_method;
			
})